library(readxl)
library(ggplot2)
library(dplyr)
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
Pulling in the datasets
## Download the average temperature file
temp_data <- read_excel("C:\\Users\\Reuben Decker\\Downloads\\average_temp.xlsx")
## Seeing the Excel file
head(temp_data)
tail(temp_data)
## Download the number of sunspots file
sunspot_data <- read_excel("C:\\Users\\Reuben Decker\\Downloads\\average_number_sunspots.xlsx")
## Seeing the Excel file
head(sunspot_data)
tail(sunspot_data)
## Download the distance the north pole traveled file
dist_data <- read_excel("C:\\Users\\Reuben Decker\\Downloads\\distance_traveling.xlsx")
## Seeing the Excel file
head(dist_data)
tail(dist_data)
## I want to merge the three datasets I have into one. I want them to match up based upon their years because this data is over time.
## Merging the datasets to match by the "Year" column
merged_data <- merge(temp_data, dist_data, by = "Year", all = TRUE)
merged_data <- merge(merged_data, sunspot_data, by = "Year", all = TRUE)
##(merged_data)
head(merged_data)
tail(merged_data)
## I only want the data where there is an average temperature available and average reported kilometers.
merged_data <- merged_data[complete.cases(merged_data$`Average Temp (F)`, merged_data$`Average Kilometers`),]
merged_data <- merged_data %>% select(-'5 Year Total Kilometers')
head(merged_data)
tail(merged_data)
Plotting Scatter Plots
## Plot the data using a scatter plot.
ggplot(merged_data, aes(x = `Average # Sunspots`, y = `Average Kilometers`, color = `Average Temp (F)`)) +
geom_point() +
labs(title = "Relationship Between Average # Sunspots, Average Kilometers, and Average Temp",
x = "Average # Sunspots",
y = "Average Kilometers")

## I figured there might be a better way to show these relationships in a 3D format and I can see clearly a relationship between these three variables.
library(plotly)
Warning: package 'plotly' was built under R version 4.3.2
Attaching package: 'plotly'
The following object is masked from 'package:ggplot2':
last_plot
The following object is masked from 'package:stats':
filter
The following object is masked from 'package:graphics':
layout
plot_ly(merged_data, x = ~`Average # Sunspots`, y = ~`Average Kilometers`, z = ~`Average Temp (F)`, color = ~`Average Temp (F)`) %>%
layout(scene = list(xaxis = list(title = "Average # Sunspots"),
yaxis = list(title = "Average Kilometers"),
zaxis = list(title = "Average Temp (F)")))
No trace type specified:
Based on info supplied, a 'scatter3d' trace seems appropriate.
Read more about this trace type -> https://plotly.com/r/reference/#scatter3d
No scatter3d mode specifed:
Setting the mode to markers
Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode